package cn.egame.file.util;

import java.io.BufferedReader;
import java.io.FileReader;
import org.apache.log4j.Logger;

public class FileUtils {
    private static final Logger logger = Logger.getLogger(FileUtils.class);

    /**
     * @Description: 每次删除100行指定文件的程序
     * @param file
     * @param num
     * @return void
     * @throws Exception
     * @throws
     * @Author yujm
     * @Create Date 2015年3月13日
     * @Modified by none
     * @Modified Date
     */
    public static void proceeDeleteFile(String path, int num) throws Exception {
        FileReader fr = null;
        BufferedReader br = null;
        try {
            fr = new FileReader(path);
            br = new BufferedReader(fr);
            int readLineNum = 0;
            int processCount = 1;
            String line = "";
            StringBuffer sb = new StringBuffer();
            while ((line = br.readLine()) != null) {
                readLineNum++;
                sb.append(line + "\r\n");
                if (readLineNum % num == 0) {
                    logger.info("====第" + processCount + "次处理=====");
                    System.out.println(sb);
                    sb.delete(0, sb.length());
                    logger.info("=====第" + processCount + "次处理结束=====");
                    processCount++;
                }
            }
            if (readLineNum % num > 0) {
                logger.info("====第" + processCount + "次处理=====");
                System.out.println(sb);
                sb.delete(0, sb.length());
                logger.info("=====第" + processCount + "次处理结束=====");
            }
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            if (null != br) {

                br.close();
            }
            if (null != fr) {
                fr.close();
            }
        }

    }
}
